<template>
    <div class="left">
        <div class="card"> 
            <div class="card_title"><i></i>{{value}}试验站盐碱地概况<i class="more_icon"></i></div>
            <div class="left1">
                <img src="~@static/img/Rectangle2389.png" alt="">
                <p>地处黄河三角洲核心，属滨海盐碱类型区，地下水埋深浅、矿化度高，土体盐分重，以氯化物为主，受海潮影响大，是我国乃至世界范围利用难度最大的三角洲型盐碱地之一。</p>
                <p>东营市立足黄河三角洲盐碱特色，发挥黄河三角洲农业高新技术产业示范区优势，初步探索出了一条“以盐适种、生态优先、用养结合、提质增效”的可借鉴、可复制推广的盐碱地综合利用特色路子，为我国盐碱地综合治理与科学利用作出了示范。</p>
            </div>
        </div>
        <div class="card mt14"> 
            <div class="card_title"><i></i>{{value}}试验站盐碱地改良后对比</div>
            <div id="lchart1"></div>
        </div>
        <div class="card mt14"> 
            <div class="card_title"><i></i>{{value}}试验站盐碱地土壤评估</div>
            <div id="lchart2"></div>
            <div class="left3_bottom flex">
                <img src="~@static/img/Group420.png" alt="">
                <p class="flex">
                    <span>土壤PH值</span>
                    <span>8.0-8.5</span>
                </p>
            </div>
            <div class="left3_bottom flex">
                <img src="~@static/img/Group421.png" alt="">
                <p class="flex">
                    <span>排水性能</span>
                    <span>较差</span>
                </p>
            </div>
        </div>
    </div>
</template>

<script>
import "echarts-gl"; 
export default {
    data(){
        return{
            value: "东营",
        }
    },
    mounted() {
        if(this.$parent.value !== '全国'){
            this.value = this.$parent.value;
        }
        this.$bus.$on("change-select",(res)=>{
            this.changeSelect(res);
        })
        this.initChart1();
        this.initChart2();
    },
    methods: {
        changeSelect(val){  
            this.value = val;
        },
        initChart2(){ 
            let _this = this;
            var chartDom = document.getElementById("lchart2");
            var chart = _this.$echarts.init(chartDom);
            chart.clear();
            chart.resize(); 
            function getParametricEquation(startRatio, endRatio, isSelected, isHovered, k,height) { 
                let midRatio = (startRatio + endRatio) / 2; 
                let startRadian = startRatio * Math.PI * 2;
                let endRadian = endRatio * Math.PI * 2;
                let midRadian = midRatio * Math.PI * 2; 
                // 如果只有一个扇形，则不实现选中效果。
                if (startRatio === 0 && endRatio === 1) { isSelected = false; } 
                // 通过扇形内径/外径的值，换算出辅助参数 k（默认值 1/3）
                k = typeof k !== 'undefined' ? k : 1 / 3 ;

                // 计算选中效果分别在 x 轴、y 轴方向上的位移（未选中，则位移均为 0）
                let offsetX = isSelected ? Math.cos(midRadian) * 0.1 : 0;
                let offsetY = isSelected ? Math.sin(midRadian) * 0.1 : 0;

                // 计算高亮效果的放大比例（未高亮，则比例为 1）
                let hoverRate = isHovered ? 1.05 : 1;

                // 返回曲面参数方程
                return { 
                    u: { min: -Math.PI, max: Math.PI * 3, step: Math.PI / 32 },
                    
                    v: { min: 0, max: Math.PI * 2, step: Math.PI / 20 },
                    
                    x: function(u, v) {
                        if (u < startRadian) {
                            return offsetX + Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate;
                        }
                        if (u > endRadian ){
                            return offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate;
                        }
                        return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate;
                    },
                    
                    y: function(u, v) {
                        if (u < startRadian) { return offsetY + Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate; }
                        if (u > endRadian ){ return offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate; }
                        return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate;
                    },
                    
                    z: function(u, v) {
                        if (u < - Math.PI * 0.5 ) { return Math.sin(u); }
                        if (u > Math.PI * 2.5 ){ return Math.sin(u); }
                        return Math.sin(v) > 0 ? 1*height : -1;
                    }
                };
            }

            // 生成模拟 3D 饼图的配置项
            function getPie3D(pieData, internalDiameterRatio) { 
                let series = [];
                let sumValue = 0;
                let startValue = 0;
                let endValue = 0;
                let legendData = [];
                let k = typeof internalDiameterRatio !== 'undefined' ? (1 - internalDiameterRatio) / (1 + internalDiameterRatio): 1 / 3;

                // 为每一个饼图数据，生成一个 series-surface 配置
                for (let i = 0; i < pieData.length; i++) {

                    sumValue += pieData[i].value;

                    let seriesItem = {
                        name: typeof pieData[i].name === 'undefined' ? `series${i}` : pieData[i].name,
                        type: 'surface',
                        parametric: true,
                        wireframe: {
                            show: false
                        },
                        pieData: pieData[i],
                        pieStatus: {
                            selected: false,
                            hovered: false,
                            k: k
                        }
                    };

                    if (typeof pieData[i].itemStyle != 'undefined') {

                        let itemStyle = {};

                        typeof pieData[i].itemStyle.color != 'undefined' ? itemStyle.color = pieData[i].itemStyle.color : null;
                        typeof pieData[i].itemStyle.opacity != 'undefined' ? itemStyle.opacity = pieData[i].itemStyle.opacity : null;

                        seriesItem.itemStyle = itemStyle;
                    }
                    series.push(seriesItem);
                }

            // 使用上一次遍历时，计算出的数据和 sumValue，调用 getParametricEquation 函数，
            // 向每个 series-surface 传入不同的参数方程 series-surface.parametricEquation，也就是实现每一个扇形。
                for (let i = 0; i < series.length; i++) {
                    endValue = startValue + series[i].pieData.value; 
                    series[i].pieData.startRatio = startValue / sumValue;
                    series[i].pieData.endRatio = endValue / sumValue;
                    series[i].parametricEquation = getParametricEquation(series[i].pieData.startRatio, series[i].pieData.endRatio, false, false, k,series[i].pieData.value);

                    startValue = endValue;

                    legendData.push(series[i].name);
                }



                        // 准备待返回的配置项，把准备好的 legendData、series 传入。
                let option = {
                    tooltip: {
                        formatter: params => {
                    if (params.seriesName !== 'mouseoutSeries') {
                        return `${params.seriesName}<br/><span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${params.color};"></span>${option.series[params.seriesIndex].pieData.value}`;
                    }
                }
            },
            legend: {
                data: legendData,
                itemWidth: 10,
                itemHeight: 10,
                orient: 'vertical',
                right: '6%',
                textStyle:{
                    color:'#fff',
                    fontSize:14
                },
                formatter: function (name) { 
                    var legendName;
                    pieData.forEach(item => {
                        if(item.name === name){ 
                            legendName = `${name} ${item.value * 10}%`;
                        }
                    });

                    return legendName;
                }
            },
                    xAxis3D: { min: -1, max: 1 },
                    yAxis3D: { min: -1, max: 1 },
                    zAxis3D: { min: -1, max: 1 },
                    grid3D: {
                        show: false,
                        boxHeight: 20,
                        //top: '30%',
                        left: "-10%",
                        // bottom: '50%',
                        // environment: '#021041',
                        viewControl:{
                            distance:160,
                            alpha: 35,
                            beta:130,
                        },
                        
                    },
                    series: series
                };
                return option;
            }

            // 传入数据生成 option
            var option = getPie3D([
                { name: '土壤质地', value: 0.5, itemStyle: { opacity: 0.7, color:'rgba(60,212,149,.8)', } } ,
                { name: '重金属', value: 0.7, itemStyle: { opacity: 0.7, color:'rgba(190,229,251,.8)', } } ,
                { name: '有机物', value: 1.3, itemStyle: { opacity: 0.7, color:'rgba(50,84,221,.8)', } } ,
                { name: '含水量', value: 2.5, itemStyle: { opacity: 0.7, color:'rgba(72,229,229,.8)', } } , 
                { name: '盐分', value: 5, itemStyle: { opacity: 0.7, color:'rgba(43,142,243,.8)', } }

            ],2);



  
            chart.setOption(option);
        },
        initChart1(){ 
            let _this = this;
            var chartDom = document.getElementById("lchart1");
            var chart = _this.$echarts.init(chartDom);
            chart.clear();
            chart.resize();
            const myData = [ '盐分', '土壤水分', '植被覆盖率', '作物生长情况', ];

            const vehicle = [389, 259, 262, 324];
            const personnel = [121, 388, 233, 309];
            var option = { 
                tooltip: {
                    show: true,
                },
                legend: {
                    top: "0", 
                    left: "43%",
                    itemWidth: 10,
                    itemHeight: 10, 
                    textStyle : {
                        color : '#ffffff',
                        fontSize : 13,
                    },
                },
                grid: [
                    {
                        show: false,
                        left: '8%',
                        top: "14%",
                        bottom: 22,
                        width: '15%',
                    },
                    {
                        show: false,
                        left: '28%',
                        top: "14%",
                        bottom: 0,
                        containLabel: true,
                        width: '34%',
                    },
                    {
                        show: false,
                        right: '4%',
                        top: "14%",
                        bottom: 0,
                        containLabel: true,
                        width: '34%',
                    },
                ],
                xAxis: [
                    {
                        gridIndex: 0,
                        show: false,
                    },
                    {
                        gridIndex: 1,
                        type: 'value',
                        inverse: true,
                        axisLine: {
                            show: false,
                        },
                        axisTick: {
                            show: false,
                        },
                        axisLabel: {
                            show: true,
                            verticalAlign: 'middle',
                            textStyle: {
                                color: '#B2B2B2',
                                fontSize: 12,
                            },
                        },
                        splitLine: {
                            show: true,
                            lineStyle:{
                                color:"rgba(108,128,151,0.3)"
                            }
                        },
                    },
                    {
                        gridIndex: 2,
                        type: 'value',
                        axisLine: {
                            show: false,
                        },
                        axisTick: {
                            show: false,
                        },
                        axisLabel: {
                            show: true,
                            verticalAlign: 'middle',
                            textStyle: {
                                color: '#B2B2B2',
                                fontSize: 12,
                            },
                        },
                        splitLine: {
                            show: true,
                            lineStyle:{
                                color:"rgba(108,128,151,0.3)"
                            }
                        },
                    },
                ],
                yAxis: [
                    {
                        gridIndex: 0,
                        type: 'category',
                        inverse: true, 
                        position: 'right',
                        axisLine: {
                            show: false, 
                        },
                        axisTick: {
                            show: false,
                        },
                        data: myData.map(function (value) {
                            return {
                                value: value,
                                textStyle: {
                                    align: 'right',
                                },
                            };
                        }),
                        axisLabel: {
                            color: "#fff",
                            fontSize: 13,
                            align: 'left', 
                        }, 
                    },
                    {
                        gridIndex: 1,
                        type: 'category',
                        inverse: true,
                        position: 'right',
                        axisLine: {
                            show: false,
                        },
                        axisTick: {
                            show: false,
                        },
                        axisLabel: {
                            show: false,
                            margin: 8,
                            textStyle: {
                                color: '#fff',
                                fontSize: 12,
                            },
                        },
                        data: myData,
                    },
                    {
                        gridIndex: 2,
                        type: 'category',
                        inverse: true,
                        position: 'left',
                        axisLine: { 
                            lineStyle: {
                            width: 3,
                            color: '#979797'
                            }
                        },
                        axisTick: {
                            show: false,
                        },
                        axisLabel: {
                            show: false,
                            textStyle: {
                                color: '#9D9EA0',
                                fontSize: 12,
                            },
                        },
                        data: myData,
                    },
                ],
                series: [
                    {
                        name: '改良前',
                        type: 'bar',
                        barGap: 20,
                        barWidth: 10,
                        xAxisIndex: 1,
                        yAxisIndex: 1,
                        showBackground: true,
                        backgroundStyle: {
                            color: 'rgba(180, 180, 180, 0.2)',
                        },
                        label: {
                            normal: {
                                show: false,
                            },
                            emphasis: {
                                show: true,
                                position: 'left',
                                offset: [0, 0],
                                textStyle: {
                                    color: '#666666',
                                    fontSize: 14,
                                },
                            },
                        },
                        itemStyle: {
                            normal: {
                                barBorderRadius: [10, 0, 0, 10],
                                color: new _this.$echarts.graphic.LinearGradient(1, 0, 0, 0, [
                                    {
                                        offset: 0,
                                        color: 'rgba(208,222,238,0)',
                                    },
                                    {
                                        offset: 1,
                                        color: 'rgba(208,222,238, 1)',
                                    },
                                ]),
                            },
                        },
                        data: vehicle,
                    },

                    {
                        name: '改良后',
                        type: 'bar',
                        barGap: 20,
                        barWidth: 10,
                        xAxisIndex: 2,
                        yAxisIndex: 2,
                        showBackground: true,
                        backgroundStyle: {
                            color: 'rgba(180, 180, 180, 0.2)',
                        },
                        label: {
                            normal: {
                                show: false,
                            },
                            emphasis: {
                                show: true,
                                position: 'right',
                                offset: [0, 0],
                                textStyle: {
                                    color: '#666666',
                                    fontSize: 14,
                                },
                            },
                        },
                        itemStyle: {
                            normal: {
                                barBorderRadius: [0, 10, 10, 0], 
                                color: new _this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                                    {
                                        offset: 0,
                                        color: 'rgba(21,154,255,0)',
                                    },
                                    {
                                        offset: 1,
                                        color: 'rgba(21,154,255, 1)',
                                    },
                                ]),
                            },
                        },
                        data: personnel,
                    },
                ],
            };
  
            chart.setOption(option);
        },
    },
}
</script>


<style lang="scss" scoped> 
@import "@/assets/styles/overView.scss"; 
.left{
    z-index: 11;
}
.mt14{
    margin-top: 14px;
} 
.left1{
    padding: 6px 16px;
    img{
        height: 100px;
        width: 100%;
    }
    p{
        text-indent: 2em;
        color: #fff;
        font-size: 13px;
    }
}
.left3_bottom{
    padding: 0 16px;
    img{
        width: 32px;
        height: 32px;
    }
    p{
        width: calc(100% - 42px);
        margin-left: 10px;
        color: #fff;
        font-size: 14px;
        justify-content: space-between;
    }
}
.left .card:nth-child(2){
    height: calc((100% - 410px) * 0.45);
    >div:nth-child(2){
        height: calc(100% - 30px); 
    }
}
.left .card:nth-child(3){
    height: calc((100% - 410px) * 0.55);
    >div:nth-child(2){
        height: calc(100% - 94px); 
    }
}
</style>